package client.gui;

import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import client.MainController;
import client.gui.panels.PanelIF;

public class MainFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * the instance of the main controller of this process
	 */
	private MainController mainController;
	
	/**
	 * the content panel holds any other panels it is given
	 */
	private JPanel contentPanel;

	/**
	 * Create the frame.
	 * @param panel is the first panel to be displayed 
	 */
	public MainFrame(MainController mainController) {
		this.mainController = mainController;
		initGui();
	}
	
	/**
	 * initialize the frame 
	 */
	private void initGui() {
		setTitle("ICM");
		setResizable(true);
		setVisible(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(10, 10, 450, 230);
		contentPanel = new JPanel();
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPanel.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPanel);
	}
	
	
	/**
	 * set a given panel to be displayed
	 * @param panel to display
	 */
	public void displayPanel(PanelIF panel) {
		contentPanel.removeAll();
		contentPanel.add((Component) panel);
		setVisible(true);
	}

	/**
	 * display a pop-up message with the given string 
	 * @param string
	 */
	public void showSplashMessage(String string) {
		JOptionPane.showMessageDialog(this, string);
	}

	/**
	 * display a pop-up error message with the given string 
	 * @param string
	 */
	public void showErrorMessage(String string) {
		JOptionPane.showMessageDialog(this, string, "Error", JOptionPane.ERROR_MESSAGE);
	}

	/**
	 * get the user's first name. used mostly by the main panel
	 * @return
	 */
	public String getUserFirstName() {
		return mainController.getUserFirstName();
	}
	
	/**
     * Re-set the main frame's bounds
     * @param x
     * @param y
     * @param width
     * @param height
     */
    public void resetBounds(int x, int y, int width, int height) {
            setBounds(x, y, width, height);
    }
	    
	/**
	 * Send login request with user login details 
	 * @param username
	 * @param password
	 */
	public void loginRequest(String username, String password) {
		mainController.loginRequest(username, password);
	}

	/**
	 * User logout method
	 */
	public void logout() {
		mainController.logoutRequest();
//		resetBounds(100, 100, 450, 230);
//		mainController.initLoginUI();
		System.exit(0);
	}
	

	
}
